<template>
  <div class="index">
    <head-text titleText="角色管理"></head-text>
    <div class="list-content">
      <div class="filter-area">
        <div>
          <el-button @click="add" plain type="success" size="mini"
            >添加角色</el-button
          >
        </div>
      </div>
      <div class="table-title">
        <span></span>
        <h5>角色列表</h5>
      </div>
      <custom-table
        :tableData="tableData"
        :tableListData="tableListData"
        :tableBtn="tableBtn"
        :currentData="currentData"
        @onPageChange="onPageChange"
        @onSizeChange="onSizeChange"
        @handleView="handleView"
        @handleViewDelete="handleViewDelete"
      ></custom-table>
    </div>
    <dialog-com :dialogObj="dialogObj" @reflash="getList"></dialog-com>
  </div>
</template>

<script>
import customTable from "@c/table";
import headText from "@c/headText";
import dialogCom from "./dialogCom";
import { getRoleListApi, getsysPermissionDeleteListApi } from "@s/api";
export default {
  name: "index",
  data() {
    return {
      nowPage: 1,
      limit: 10,
      currentData: {},
      dialogObj: {
        id: "",
        view: false,
        title: "",
        show: false,
        form: {}
      },
      tableData: []
    };
  },
  components: {
    headText,
    dialogCom,
    customTable
  },
  created() {
    //  table表格
    this.tableListData = [
      { width: "50", label: "", type: "index" },
      { prop: "name", width: "", label: "角色名称" },
      { prop: "description", width: "", label: "备注" },
      { label: "操作", type: "btn", width: "180", fixed: "right" }
    ];
    // 按钮
    this.tableBtn = [
      {
        name: "编辑",
        btnType: "primary",
        handleFn: "handleView"
      },
      {
        name: "删除",
        btnType: "primary",
        handleFn: "handleViewDelete"
      }
    ];
  },
  mounted() {
    this.getList();
  },
  methods: {
    onPageChange(val) {
      this.nowPage = val;
      this.getList();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.getList();
    },
    //获取角色列表
    getList() {
      let json = {
        limit: this.limit,
        page: this.nowPage
      };
      getRoleListApi(json).then(res => {
        this.currentData.total = res.total;
        this.tableData = res.list;
      });
    },
    handleViewDelete(v) {
      this.$confirm("此操作将删除该角色?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        getsysPermissionDeleteListApi([v.roleId]).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功！"
          });
        });
      });
    },
    add() {
      this.dialogObj.id = "";
      this.dialogObj.view = false;
      this.dialogObj.title = "添加角色";
      this.dialogObj.show = true;
    },
    handleView(v) {
      this.dialogObj.id = v.roleId;
      this.dialogObj.form = v;
      this.dialogObj.view = false; // 编辑
      this.dialogObj.title = "编辑权限";
      this.dialogObj.show = true;
    }
  }
};
</script>
<style scoped lang="scss"></style>
